package com.android.aishangauction.fragment.group;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.aishangauction.activity.MainTab;
import com.android.aishangauction.adapt.MainNationAdapt;
import com.android.aishangauction.utils.ObjectUtils;
import com.android.aishangauction.R;
import com.android.aishangauction.adapt.MainListSortBrandAdapt;
import com.android.aishangauction.cache.DiskLruCacheHelper;
import com.android.aishangauction.fragment.BaseLazyFragment;
import com.android.aishangauction.model.group.SortBrandData;
import com.android.aishangauction.model.group.SortBrandDataCallback;
import com.android.aishangauction.model.group.SortNationData;
import com.android.aishangauction.model.group.SortNationDataCallback;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 海淘  大牌 分类展示
 * ---------------------------------------
 */
public class Fragment_group_BrandNation_Sort extends BaseLazyFragment {


    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;

    private static final String ARG_PARAM1 = "param1";
    private int type;
    private View rootView;
    private boolean networkConnected;
    private LayoutInflater inflater;
    private FragmentActivity mcontext;

    private DiskLruCacheHelper helper;
    private MainListSortBrandAdapt adapter;
    private MainNationAdapt adapter_nation;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(ARG_PARAM1);

        }
    }

    public static Fragment_group_BrandNation_Sort newInstance(int type) {
        Fragment_group_BrandNation_Sort fragment = new Fragment_group_BrandNation_Sort();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initPrepare() {

        switch (type) {
            case 0:
                adapter = new MainListSortBrandAdapt(R.layout.item_list_sort_dapai, null);
                adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                mRecyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                        Intent intent = new Intent(mcontext, MainTab.class);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, position);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 0);
                        startActivity(intent);
                    }
                });

                break;
            case 1:
                View head = inflater.inflate(R.layout.item_list_sort_dapai, null, false);
                head.findViewById(R.id.ll_img).setVisibility(View.GONE);
                head.findViewById(R.id.ll_nation_logo).setVisibility(View.VISIBLE);

                adapter_nation = new MainNationAdapt(R.layout.item_list_sort_dapai, null);
                adapter_nation.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                adapter_nation.addHeaderView(head);
                mRecyclerView.setAdapter(adapter_nation);
                adapter_nation.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                        Intent intent = new Intent(mcontext, MainTab.class);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, position);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 1);
                        startActivity(intent);
                    }
                });

                break;
        }

        //刷新
        Refresh();


    }

    @Override
    public void onStart() {
        super.onStart();

        if (type == 0) {
            GetListData_Brand();
        }

    }


    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {

        if (type == 1) {
            GetListData_Nation();
        }


    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        rootView = inflater.inflate(R.layout.layout_recycle_refresh, container, false);
        ButterKnife.bind(this, rootView);
        mcontext = getActivity();
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        initUI();

        return rootView;
    }

    private void initUI() {
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mainTopBg.setVisibility(View.GONE);
        rootView.findViewById(R.id.top).setVisibility(View.INVISIBLE);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mcontext);
        mRecyclerView.setLayoutManager(mLayoutManager);
    }


    private void Refresh() {
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                if (networkConnected) {

                    switch (type) {
                        case 0:
                            GetListData_Brand();
                            break;
                        case 1:
                            GetListData_Nation();
                            break;
                    }

                } else {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.toast(mcontext, "网络出现异常");
                }

            }
        });

    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        switch (type) {
            case 0:
                GetListData_Brand();
                break;
            case 1:
                GetListData_Nation();
                break;
        }

    }

    //超值大牌
    private void GetListData_Brand() {

        String address = ConstantsUrl.domain + ConstantsUrl.MAINLIST_DAPAI;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, new SortBrandDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SortBrandData response, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_DAPAI, response.toString());
                        ArrayList<SortBrandData.DataBean.ListBean> listdata = new ArrayList<>();
                        if (response.getData() == null) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        listdata.addAll(response.getData().getList());
                        adapter.setNewData(listdata);

                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    //海淘
    private void GetListData_Nation() {
        String address = ConstantsUrl.domain + ConstantsUrl.NATIONLISTDATA;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, new SortNationDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SortNationData response, int id) {
                    mRefreshLayout.setRefreshing(false);
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_NATION, response.toString());
                        ArrayList<SortNationData.Nationdata> listleftdata = new ArrayList<>();
                        if (response.getData() == null) {
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }

                        listleftdata.addAll(response.getData());
                        adapter_nation.setNewData(listleftdata);


                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");

        }
    }


}
